<?php
	class Dumper {
		//var $base_url = "http://www.ceic.ac.cn/ajax/speedsearch?num=6&&page={page}";
		var $startd = "2016-01-01";
		var $endd = "2016-12-31";
		
		var $base_url = "http://www.ceic.ac.cn/ajax/search?page={page}&&start={start}&&end={end}&&jingdu1=&&jingdu2=&&weidu1=&&weidu2=&&height1=&&height2=&&zhenji1=&&zhenji2=";
		var $page = 1;
		var $path = 'data/';

		function getUrl($p){
			return str_replace('{end}',$this->endd,str_replace('{page}',$p,str_replace('{start}',$this->startd,$this->base_url)));
		}
		
		function download($p){
			$url = $this->getUrl($p);
			$content = file_get_contents($url) or die();
			$content = substr($content,1,strlen($content)-2);
			$json = json_decode($content,true);
			$data = $json['shuju'];
			unset($data['page']);
			$file = $this->path . $this->startd . "_" . $this->endd . "_p_{$p}.json";
			file_put_contents($file,json_encode($data));
			echo "Page {$p}/{$json['num']} Downloaded.\r\n";
			if($p == $json['num']){
				return false;
			}
			return true;
		}
		
		function run(){
			$this->page = 1;
			$this->startd = "2011-01-01";
			$this->endd = "2011-12-31";
			
			while($this->download($this->page)){
				$this->page++;
				sleep(2);
			}
		}
		
		function __construct(){
			@mkdir($this->path);
		}
	}
	$dumper = new Dumper();
	$dumper->run();

?>